package com.chen.corejava.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;

/**
 *视图
 *@author：email: <a href="cyb@ewppay.com"> Administrator </a> 
 *@Date 2017-2-21 下午3:06:27 
 */

public class ViewTest<E> {

	/**
	 * @param @param args   
	 * @return void  
	 * @throws
	 * @author cyb
	 * @date 2017-2-21
	 */
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		List<String> unodifiList = Collections.unmodifiableList(list);
		for(String s : unodifiList){
			System.out.println(s);
		}
		
		//unodifiList.add("d");//java.lang.UnsupportedOperationException
		//unodifiList.set(0, "e");//java.lang.UnsupportedOperationException
		String[] cards = new String[]{"a","b","c"};
		List<String> cardsList = Arrays.asList(cards);
		System.out.println(cardsList.get(0));
		cardsList.set(0, "e");
		System.out.println(cardsList.get(0));
		//cardsList.add("f");//java.lang.UnsupportedOperationException
		
		
		List<String> settings = Collections.nCopies(100, "DEFAULT");
		//System.out.println(settings.get(0));
		//settings.set(0, "abc");//java.lang.UnsupportedOperationException
		//settings.add("efg");//java.lang.UnsupportedOperationException
		
		ViewTest<String> test = new ViewTest<String>(5);
		test.add("1");
		test.add("2");
		String[] arry = test.toArray(new String[0]);
		for(String s : arry){
			System.out.print(s + ",");
		}
		System.out.println();
		String[] arry4 = test.toArray(new String[]{"3"});//<
		for(String s : arry4){
			System.out.print(s + ",,,,");
		}
		
		System.out.println();
		String[] arry5 = test.toArray(new String[2]);//=
		for(String s : arry5){
			System.out.print(s + ",,");
		}
		
		System.out.println();
		String[] arry2 = test.toArray(new String[]{"3","4"});//=
		for(String s : arry2){
			System.out.print(s + ",a,");
		}
		System.out.println();
		String[] arry3 = test.toArray(new String[]{"3","4","5","6"});//>
		for(String s : arry3){
			System.out.print(s + ",,,");
		}
//		Collections.max(strList);
//		int n = Collections.binarySearch(strList, "b");
//		System.out.println(n + ",");
//		Hashtable t = new Hashtable();
//		t.elements();
	}
	
	public static void test(){
		//数组和集合的转换
		List<String> strList = new ArrayList<String>();
		strList.add("aa");
		strList.add("bb");
		String[] arry = strList.toArray(new String[0]);
		for(String s : arry){
			System.out.print(s + ",");//aa,bb,
		}
		System.out.println();
		String[] arry4 = strList.toArray(new String[]{"ee"});//<
		for(String s : arry4){
			System.out.print(s + ",,,,");
		}
		
		System.out.println();
		String[] arry5 = strList.toArray(new String[strList.size()]);//=
		for(String s : arry5){
			System.out.print(s + ",,");
		}
		
		System.out.println();
		String[] arry2 = strList.toArray(new String[]{"ee","dd"});//=
		for(String s : arry2){
			System.out.print(s + ",5,");
		}
		System.out.println();
		String[] arry3 = strList.toArray(new String[]{"ee","dd","ff","gg"});//>
		for(String s : arry3){
			System.out.print(s + ",,,");
		}
	}
	
	 public  <T> T[] toArray(T[] a) {
	        if (a.length < size)
	            // Make a new array of a's runtime type, but my contents:
	            return (T[]) Arrays.copyOf(elementData, size, a.getClass());
	        System.arraycopy(elementData, 0, a, 0, size);
	        if (a.length > size)
	            a[size] = null;
	        return a;
	    }
	 private int size;
	 
	 private transient Object[] elementData;
	 
	 public boolean add(E e) {
	        elementData[size++] = e;
	        return true;
	    }
	 
	 public ViewTest(int initialCapacity) {
	        super();
	        if (initialCapacity < 0)
	            throw new IllegalArgumentException("Illegal Capacity: "+
	                                               initialCapacity);
	        this.elementData = new Object[initialCapacity];
	 }
}
